<?php

/**
 * This is the model class for table "taxonomy_term".
 */
require_once(dirname(__FILE__) . '/base/TermBase.php');
class Term extends TermBase
{
    const STATE_ACTIVE = 1, STATE_DEACTIVE = 2;

    public $parentId;

    public $names;

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
            array('v_id, name, alias', 'required', 'on' => 'single_save'),
            array('v_id, names', 'required', 'on' => 'bulk_save'),
            array('state, parentId', 'numerical', 'integerOnly' => true),
            array('name, alias', 'length', 'max' => 255),
            array('description, ordering', 'safe'),
            array('id, v_id, name, alias, description, ordering, state, parentId', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        return array(
            'TermHierarchy' => array(self::HAS_ONE, 'TermHierarchy', 'parent_id', 'joinType' => 'INNER JOIN'),
            'children' => array(self::HAS_MANY, 'Term', 'term_id', 'through' => 'TermHierarchy', 'joinType' => 'INNER JOIN'),
            'TermHierarchy2' => array(self::HAS_ONE, 'TermHierarchy', 'term_id', 'joinType' => 'INNER JOIN'),
            'parent' => array(self::HAS_MANY, 'Term', 'parent_id', 'through' => 'TermHierarchy2', 'joinType' => 'INNER JOIN'),
            'vocabulary' => array(self::BELONGS_TO, 'Vocabulary', 'v_id'),
            'object' => array(self::HAS_MANY, 'TermRelation', 'term_id'),
        );
    }

    public function getStateOptions()
    {
        return array(
            self::STATE_ACTIVE => 'Active',
            self::STATE_DEACTIVE => 'Deactive',
        );
    }

    public function getStateText($id = null)
    {
        if (is_null($id))
            $id = $this->state;

        $options = $this->getStateOptions();
        return isset($options[$this->state]) ? $options[$this->state] : '';
    }
}